<?php

class Application_Model_Filter_StoreItemSearch {
      /*
       * 快速搜索
       */

      public $quick;

      /**
       * 开始时间
       */
      public $dateinbegin;

      /**
       * 结束时间
       */
      public $dateinend;

      /**
       * 左边状态
       */
      public $stus;

      /**
       * 内部状态
       */
      public $interior;

      /**
       * 今天
       */
      public $day;

      /**
       * 是否删除
       */
      public $isdel;

      /**
       * 历史库存
       */
      public $type;

      /**
       * 是否扫描
       */
      public $notscan;

      /**
       * 物品来源
       */
      public $intype;

      /**
       * 开始净重
       */
      public $startWeight;

      /**
       * 结束净重
       */
      public $endWeight;

      /**
       * 开始体积
       */
      public $startVolume;

      /**
       * 结束体积
       */
      public $endVolume;

      /**
       * 快递公司
       */
      public $shiptype;

      /**
       * 特殊包裹
       */
      public $special;

      /**
       * 现有与历史
       */
      public $history;

      /**
       * 是否到付
       */
      public $isShipFee;

      /**
       * 问题包裹
       */
      public $problem;

      /**
       * 操作人
       */
      public $operateName;
      /**
       * 用户ID
       */
      public $userId;
      /**
       * 物品名称
       */
      public $productName;
      /**
       * 物流公司
       */
      public $logisticsName;
      /**
       * 状态
       */
      public $state;

      function getSearchFileds() {
            $where = array();
            $and = '';
            $userarrid = "";
            $pd = true;
            if ($this->quick != NULL) {
                  $and = "(";
                  $userItem = Application_Model_User::getByNikeNameStore($this->quick);
                  if ($userItem) {
                        foreach ($userItem as $uservalue) {
                              $userarrid.= $uservalue->id . ",";
                        }
                        $where[$and . " userid in "] = "(" . $userarrid . "0)";
                        $and = " or ";
                  }
                  $firstLetter = substr($this->quick, 0, 1);
                  if (strtoupper($firstLetter) == "F") {
                        $storeno = "P" . strtoupper(substr($this->quick, 1, 5));
                  } else {
                        $storeno = $this->quick;
                  }
                  $where[$and . ' orderno like '] = '%' . $storeno . '%';
                  $and = " or ";
                  $where[$and . ' operateName like '] = '%' . $this->quick . '%';
                  $where[$and . ' barcode like '] = '%' . $this->quick . '%';
                  $where[" ) and "] = 1;
                  $and = " and ";
                  $pd = false;
            }
            if ($this->stus != 1 && $this->stus != 2) {
                  if ($this->dateinbegin != NULL) {
                        $where[$and . ' dateInStore >= '] = $this->dateinbegin;
                        $and = ' and ';
                  }

                  if ($this->dateinend != NULL) {
                        $where[$and . ' dateInStore <= '] = $this->dateinend . " 23:59:59";
                        $and = ' and ';
                  }
            } else {
                  if ($this->stus == 2) {   //出库
                        if ($this->day == 1) {
                              $where[$and . ' dateOut >= '] = date('Y-m-d'); //今日出库
                              $and = ' and ';
                        } else {
                              if ($this->dateinbegin != NULL) {
                                    $where[$and . ' dateOut >= '] = $this->dateinbegin;
                                    $and = ' and ';
                              }
                              if ($this->dateinend != NULL) {
                                    $where[$and . ' dateOut <= '] = $this->dateinend . " 23:59:59";
                                    $and = ' and ';
                              }
                        }
                  } else if ($this->stus == 1) { //入库
                        if ($this->day == 1) {
                              $where[$and . ' createdate >= '] = date('Y-m-d'); //今日入库
                              $and = ' and ';
                        } else {
                              if ($this->dateinbegin != NULL) {
                                    $where[$and . ' createdate >= '] = $this->dateinbegin;
                                    $and = ' and ';
                              }
                              if ($this->dateinend != NULL) {
                                    $where[$and . ' createdate <= '] = $this->dateinend . " 23:59:59";
                                    $and = ' and ';
                              }
                        }
                  }
            }
            if ($this->shiptype != NULL) {
                  $where[$and . ' orderNo like '] = "%" . $this->shiptype . "%";
                  $and = ' and';
            }
            if ($this->interior != NULL) {
                  $where[$and . ' interior = '] = $this->interior;
                  $and = ' and ';
            }
            if ($this->intype != NULL) {
                  $where[$and . ' intype = '] = $this->intype;
                  $and = ' and ';
            }
            if ($this->startWeight != NULL) {
                  $where[$and . ' weight >= '] = $this->startWeight * 1000;
                  $and = ' and ';
            }
            if ($this->endWeight != NULL) {
                  $where[$and . ' weight <= '] = $this->endWeight * 1000;
                  $and = ' and ';
            }
            if ($this->startVolume != NULL) {
                  $where[$and . ' volumeWeight >= '] = $this->startVolume * 1000;
                  $and = ' and ';
            }
            if ($this->endVolume != NULL) {
                  $where[$and . ' volumeWeight <= '] = $this->endVolume * 1000;
                  $and = ' and ';
            }
            if ($this->notscan == "notscan") {
                  $where[$and . '(interior= '] = Application_Model_StoreItem::Interior_HoldItem;
                  $where[' or interior= '] = Application_Model_StoreItem::Interior_InStored;
                  $where[') and (locationNo is null) and '] = 1;
                  $and = " and ";
            } else if ($this->notscan == "beyondnotscan") {
                  $time = date("Y-m-d H:i:s", time() - 3600 * 36);
                  $where[$and . '(interior= '] = Application_Model_StoreItem::Interior_HoldItem;
                  $where[' or interior= '] = Application_Model_StoreItem::Interior_InStored;
                  $where[") and dateInStore < '" . $time . "' and (locationNo is null) and "] = 1;
                  $where[" and state != "] = Application_Model_StoreItem::STATUS_Sended;
                  $and = " and ";
            }
            if ($this->special == "special") {
                  $where[$and . '(locationNo = '] = "GZ-11";
                  $where[' or locationNo  = '] = "BH-11";
                  $where[' or locationNo = '] = "TS-11";
                  $where[' or locationNo   = '] = "GQ-11";
                  $where[') and '] = 1;
                  $and = " and ";
            }
            if ($this->history == "1") {
                  $where[$and . ' (interior != '] = Application_Model_StoreItem::Interior_HoldItem;
                  $where[' or interior != '] = Application_Model_StoreItem::Interior_InStored;
                  $where[') and '] = 1;
                  $and = " and ";
            } else if ($this->history == "2") {
                  $where[$and . '(interior = '] = Application_Model_StoreItem::Interior_HoldItem;
                  $where[' or interior = '] = Application_Model_StoreItem::Interior_InStored;
                  $where[') and '] = 1;
                  $and = " and ";
            }
            if ($this->problem == 1) {
                  $where[$and . ' isProblem = '] = 1;
                  $and = " and ";
            }
            if($this->operateName != NULL){
                  $where[$and . ' operateName like '] = '%' . $this->operateName . '%';
                  $and = "and";
            }
            if($this->userId != null){
                $where[$and . " userid = "] = $this->userId;
                $and = " and ";
            }
            if($this->productName != null ){
                $where[$and . " productName = "] = $this->productName;
                $and = " and ";
            }
            if($this->logisticsName != null ){
                $where[$and . " logisticsName = "] = $this->logisticsName;
                $and = " and ";
            }
            if($this->state != null) {
                $where[$and . " state = "] = $this->state;
                $and = " and ";
            }
            if ($this->isdel != NULL) {
                  $where[$and . " isdelete = "] = $this->isdel;
                  $and = " and ";
            } else {
                  if ($this->isShipFee != null) {
                        $where[$and . ' cnShipFee > '] = 0;
                        $and = " and ";
                  }else {
                        if ($pd) {
                              $where[$and . " (isdelete is null or isdelete = 0) and "] = 1;
                              $and = " and ";
                        }
                  }
            }
            //var_dump($where);die;
            return $where;
      }


      /**
        历史库存查询
      */
       function getHistoryFilter() {
            $where = array();
            $and = '';
            $userarrid = "";
            $pd = true;
            //$time = date('Y-m-d', strtotime('- ' . 7 . ' month'));
            if ($this->quick != NULL) {
                  $and = "(";
                  $userItem = Application_Model_User::getByNikeNameStore($this->quick);
                  if ($userItem) {
                        foreach ($userItem as $uservalue) {
                              $userarrid.= $uservalue->id . ",";
                        }
                        $where[$and . " userid in "] = "(" . $userarrid . "0)";
                        $and = " or ";
                  }
                  $firstLetter = substr($this->quick, 0, 1);
                  if (strtoupper($firstLetter) == "F") {
                        $storeno = "P" . strtoupper(substr($this->quick, 1, 5));
                  } else {
                        $storeno = $this->quick;
                  }
                  $where[$and . ' orderno like '] = '%' . $storeno . '%';
                  $and = " or ";
                  $where[$and . ' operateName like '] = '%' . $this->quick . '%';
                  $where[$and . ' barcode like '] = '%' . $this->quick . '%';
                  $where[" ) and "] = 1;
                  $and = " and ";
                  $pd = false;
            }
            if ($this->stus != 1 && $this->stus != 2) {
                  if ($this->dateinbegin != NULL) {
                        $where[$and . ' dateInStore >= '] = $this->dateinbegin;
                        $and = ' and ';
                  }

                  if ($this->dateinend != NULL) {
                        $where[$and . ' dateInStore <= '] = $this->dateinend . " 23:59:59";
                        $and = ' and ';
                  }
            } else {
                  if ($this->stus == 2) {   //出库
                        if ($this->day == 1) {
                              $where[$and . ' dateOut >= '] = date('Y-m-d'); //今日出库
                              $and = ' and ';
                        } else {
                              if ($this->dateinbegin != NULL) {
                                    $where[$and . ' dateOut >= '] = $this->dateinbegin;
                                    $and = ' and ';
                              }
                              if ($this->dateinend != NULL) {
                                    $where[$and . ' dateOut <= '] = $this->dateinend . " 23:59:59";
                                    $and = ' and ';
                              }
                        }
                  } else if ($this->stus == 1) { //入库
                        if ($this->day == 1) {
                              $where[$and . ' createdate >= '] = date('Y-m-d'); //今日入库
                              $and = ' and ';
                        } else {
                              if ($this->dateinbegin != NULL) {
                                    $where[$and . ' createdate >= '] = $this->dateinbegin;
                                    $and = ' and ';
                              }
                              if ($this->dateinend != NULL) {
                                    $where[$and . ' createdate <= '] = $this->dateinend . " 23:59:59";
                                    $and = ' and ';
                              }
                        }
                  }
            }
            if ($this->shiptype != NULL) {
                  $where[$and . ' orderNo like '] = "%" . $this->shiptype . "%";
                  $and = ' and';
            }
            if ($this->interior != NULL) {
                  $where[$and . ' interior = '] = $this->interior;
                  $and = ' and ';
            }
            if ($this->intype != NULL) {
                  $where[$and . ' intype = '] = $this->intype;
                  $and = ' and ';
            }
            if ($this->startWeight != NULL) {
                  $where[$and . ' weight >= '] = $this->startWeight * 1000;
                  $and = ' and ';
            }
            if ($this->endWeight != NULL) {
                  $where[$and . ' weight <= '] = $this->endWeight * 1000;
                  $and = ' and ';
            }
            if ($this->startVolume != NULL) {
                  $where[$and . ' volumeWeight >= '] = $this->startVolume * 1000;
                  $and = ' and ';
            }
            if ($this->endVolume != NULL) {
                  $where[$and . ' volumeWeight <= '] = $this->endVolume * 1000;
                  $and = ' and ';
            }
            if ($this->notscan == "notscan") {
                  $where[$and . '(interior= '] = Application_Model_StoreItem::Interior_HoldItem;
                  $where[' or interior= '] = Application_Model_StoreItem::Interior_InStored;
                  $where[') and (locationNo is null) and '] = 1;
                  $and = " and ";
            } else if ($this->notscan == "beyondnotscan") {
                  $time = date("Y-m-d H:i:s", time() - 3600 * 36);
                  $where[$and . '(interior= '] = Application_Model_StoreItem::Interior_HoldItem;
                  $where[' or interior= '] = Application_Model_StoreItem::Interior_InStored;
                  $where[") and dateInStore < '" . $time . "' and (locationNo is null) and "] = 1;
                  $where[" and state != "] = Application_Model_StoreItem::STATUS_Sended;
                  $and = " and ";
            }
            if ($this->special == "special") {
                  $where[$and . '(locationNo = '] = "GZ-11";
                  $where[' or locationNo  = '] = "BH-11";
                  $where[' or locationNo = '] = "TS-11";
                  $where[' or locationNo   = '] = "GQ-11";
                  $where[') and '] = 1;
                  $and = " and ";
            }
            if ($this->history == "1") {
                  $where[$and . ' (interior != '] = Application_Model_StoreItem::Interior_HoldItem;
                  $where[' or interior != '] = Application_Model_StoreItem::Interior_InStored;
                  $where[') and '] = 1;
                  $and = " and ";
            } else if ($this->history == "2") {
                  $where[$and . '(interior = '] = Application_Model_StoreItem::Interior_HoldItem;
                  $where[' or interior = '] = Application_Model_StoreItem::Interior_InStored;
                  $where[') and '] = 1;
                  $and = " and ";
            }
            if ($this->problem == 1) {
                  $where[$and . ' isProblem = '] = 1;
                  $and = " and ";
            }
            if($this->operateName != NULL){
                  $where[$and . ' operateName like '] = '%' . $this->operateName . '%';
                  $and = "and";
            }
            if($this->userId != null){
                $where[$and . " userid = "] = $this->userId;
                $and = " and ";
            }
            if($this->productName != null ){
                $where[$and . " productName = "] = $this->productName;
                $and = " and ";
            }
            if($this->logisticsName != null ){
                $where[$and . " logisticsName = "] = $this->logisticsName;
                $and = " and ";
            }
            if($this->state != null) {
                $where[$and . " state = "] = $this->state;
                $and = " and ";
            }
            if ($this->isdel != NULL) {
                  $where[$and . " isdelete = "] = $this->isdel;
                  $and = " and ";
            } else {
                  if ($this->isShipFee != null) {
                        $where[$and . ' cnShipFee > '] = 0;
                        $and = " and ";
                  }else {
                              $where[$and . 'state = '] = 2;
                              $where[$and . 'interior = '] = 10;
                              $and = " and ";
                              $where[$and . '(isdelete = '] = 1;
                              $where[' or dateOver < '] = date('Y-m-d H:i:s',time());
                              $where[' or isdelete < '] = 0;
                              $where[') and '] = 1;
                              $and = " and ";
                  }
            }
            return $where;
      }

      /**
         过期与丢弃列表查询
      */
      function getdelleteSearchFileds() {
        
            $where = array();
            $and = '';
            $userarrid = "";
            $pd = true;
            //$time = date('Y-m-d', strtotime('- ' . 7 . ' month'));
            if ($this->quick != NULL) {

                  $and = "(";
                  $userItem = Application_Model_User::getByNikeNameStore($this->quick);
                  if ($userItem) {
                        foreach ($userItem as $uservalue) {
                              $userarrid.= $uservalue->id . ",";
                        }
                        $where[$and . " userid in "] = "(" . $userarrid . "0)";
                        $and = " or ";
                  }
                  $firstLetter = substr($this->quick, 0, 1);
                  if (strtoupper($firstLetter) == "F") {
                        $storeno = "P" . strtoupper(substr($this->quick, 1, 5));
                  } else {
                        $storeno = $this->quick;
                  }
                  $where[$and . ' orderno like '] = '%' . $storeno . '%';
                  $and = " or ";
                  $where[$and . ' operateName like '] = '%' . $this->quick . '%';
                  $where[$and . ' barcode like '] = '%' . $this->quick . '%';
                  $where[" ) and "] = 1;              
                  $and = " and (";
                  $where[$and . 'isdelete = '] = 1;
                  $and = " or ";
                  $where[$and . ' dateOver < '] = date('Y-m-d H:i:s');
                  $and = " ) and ";
                  $where[$and . 'state = '] = 1;
                  $pd = false;    
            }

            if ($this->stus != 1 && $this->stus != 2) {
                  if ($this->dateinbegin != NULL) {
                        $where[$and . ' dateInStore >= '] = $this->dateinbegin;
                        $and = ' and ';
                  }

                  if ($this->dateinend != NULL) {
                        $where[$and . ' dateInStore <= '] = $this->dateinend . " 23:59:59";
                        $and = ' and ';
                  }
            } else {
                  if ($this->stus == 2) {   //出库
                        if ($this->day == 1) {
                              $where[$and . ' dateOut >= '] = date('Y-m-d'); //今日出库
                              $and = ' and ';
                        } else {
                              if ($this->dateinbegin != NULL) {
                                    $where[$and . ' dateOut >= '] = $this->dateinbegin;
                                    $and = ' and ';
                              }
                              if ($this->dateinend != NULL) {
                                    $where[$and . ' dateOut <= '] = $this->dateinend . " 23:59:59";
                                    $and = ' and ';
                              }
                        }
                  } else if ($this->stus == 1) { //入库
                        if ($this->day == 1) {
                              $where[$and . ' createdate >= '] = date('Y-m-d'); //今日入库
                              $and = ' and ';
                        } else {
                              if ($this->dateinbegin != NULL) {
                                    $where[$and . ' createdate >= '] = $this->dateinbegin;
                                    $and = ' and ';
                              }
                              if ($this->dateinend != NULL) {
                                    $where[$and . ' createdate <= '] = $this->dateinend . " 23:59:59";
                                    $and = ' and ';
                              }
                        }
                  }
            }

            if ($this->shiptype != NULL) {
                  $where[$and . ' orderNo like '] = "%" . $this->shiptype . "%";
                  $and = ' and';
            }
            if ($this->interior != NULL) {
                  $where[$and . ' interior = '] = $this->interior;
                  $and = ' and ';
            }
            if ($this->intype != NULL) {
                  $where[$and . ' intype = '] = $this->intype;
                  $and = ' and ';
            }
            if ($this->startWeight != NULL) {
                  $where[$and . ' weight >= '] = $this->startWeight * 1000;
                  $and = ' and ';
            }
            if ($this->endWeight != NULL) {
                  $where[$and . ' weight <= '] = $this->endWeight * 1000;
                  $and = ' and ';
            }
            if ($this->startVolume != NULL) {
                  $where[$and . ' volumeWeight >= '] = $this->startVolume * 1000;
                  $and = ' and ';
            }
            if ($this->endVolume != NULL) {
                  $where[$and . ' volumeWeight <= '] = $this->endVolume * 1000;
                  $and = ' and ';
            }
            if ($this->notscan == "notscan") {
                  $where[$and . '(interior= '] = Application_Model_StoreItem::Interior_HoldItem;
                  $where[' or interior= '] = Application_Model_StoreItem::Interior_InStored;
                  $where[') and (locationNo is null) and '] = 1;
                  $and = " and ";
            } else if ($this->notscan == "beyondnotscan") {
                  $time = date("Y-m-d H:i:s", time() - 3600 * 36);
                  $where[$and . '(interior= '] = Application_Model_StoreItem::Interior_HoldItem;
                  $where[' or interior= '] = Application_Model_StoreItem::Interior_InStored;
                  $where[") and dateInStore < '" . $time . "' and (locationNo is null) and "] = 1;
                  $where[" and state != "] = Application_Model_StoreItem::STATUS_Sended;
                  $and = " and ";
            }
            if ($this->special == "special") {
                  $where[$and . '(locationNo = '] = "GZ-11";
                  $where[' or locationNo  = '] = "BH-11";
                  $where[' or locationNo = '] = "TS-11";
                  $where[' or locationNo   = '] = "GQ-11";
                  $where[') and '] = 1;
                  $and = " and ";
            }
            if ($this->history == "1") {
                  $where[$and . ' (interior != '] = Application_Model_StoreItem::Interior_HoldItem;
                  $where[' or interior != '] = Application_Model_StoreItem::Interior_InStored;
                  $where[') and '] = 1;
                  $and = " and ";
            } else if ($this->history == "2") {
                  $where[$and . '(interior = '] = Application_Model_StoreItem::Interior_HoldItem;
                  $where[' or interior = '] = Application_Model_StoreItem::Interior_InStored;
                  $where[') and '] = 1;
                  $and = " and ";
            }
            if ($this->problem == 1) {
                  $where[$and . ' isProblem = '] = 1;
                  $and = " and ";
            }
            if($this->operateName != NULL){
                  $where[$and . ' operateName like '] = '%' . $this->operateName . '%';
                  $and = "and";
            }
            if($this->userId != null){
                $where[$and . " userid = "] = $this->userId;
                $and = " and ";
            }
            if($this->productName != null ){
                $where[$and . " productName = "] = $this->productName;
                $and = " and ";
            }
            if($this->logisticsName != null ){
                $where[$and . " logisticsName = "] = $this->logisticsName;
                $and = " and ";
            }
            if($this->state != null) {
                $where[$and . " state = "] = $this->state;
                $and = " and ";
            }
            if ($this->isdel != NULL) {
                  $where[$and . " isdelete = "] = $this->isdel;
                  $and = " and ";
            } else {
                  if ($this->isShipFee != null) {
                        $where[$and . ' cnShipFee > '] = 0;
                        $and = " and ";
                  } else {
                        if ($pd) {
                              // $where[$and . " ( isdelete = 1 and "] = 1;
                              // $where[ "or now()-dateInStore >  "] = 518400;
                              // $where[') and '] = 1;
                              // $and = "and ";
                          //$where[$and . " state = "] = 1;
                          $where[$and . 'state = '] = 1;
                          $and = " and ";
                          $where[$and . '(isdelete = '] = 1;
                          $where[' or dateOver < '] = date('Y-m-d H:i:s',time());
                          $where[') and '] = 1;
                          $and = " and ";
                        }
                  }
            }
            //var_dump($where);die;
            return $where;
      }

//42313767249
}
